//
//  MyView.m
//  FVQuickMessage
//
//  Created by dllo on 16/8/9.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "MyView.h"
#import "HeaderTableViewCell.h"
#import "MyTableViewCell.h"
#import "MQLogonVCOfMinDy.h"
#import "AppDelegate.h"
#import "ScanViewController.h"
#import "MQDetailVCOfFind.h"
#import "FamilyViewController.h"





@interface MyView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSArray *array;

@property (nonatomic, retain) UIButton *button;

@end

@implementation MyView

-(instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

-(void)createSubView {
    
    self.array = @[@"相册", @"扫一扫", @"360全景"];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 35, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) * 2 / 3 - 64) style:UITableViewStyleGrouped];
    
    [self addSubview:_tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.scrollEnabled = NO;
    
    [_tableView registerClass:[HeaderTableViewCell class] forCellReuseIdentifier:@"headerCell"];
    [_tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"myCell"];
    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(40, self.bounds.size.height - 200, self.bounds.size.width - 80, self.bounds.size.height / 15);
    self.button.backgroundColor = [UIColor redColor];
    [self.button setTitle:@"退出登录" forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.button.titleLabel.font = [UIFont systemFontOfSize:20];
    self.button.layer.cornerRadius = 8;
    self.button.layer.masksToBounds = YES;
    [self addSubview:_button];
    
    [self.button addTarget:self action:@selector(LoginOUT) forControlEvents:UIControlEventTouchUpInside];
    
}


-(void)LoginOUT{
    
    //isUnbind是否解绑，如果是主动退出，那么传YES 被迫传NO
    
    [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES completion:^(NSDictionary *info, EMError *error) {
        
        if (!error) {
            
            AppDelegate *app = [UIApplication sharedApplication].delegate;
            
            [app  logOutSuccess];
            
        }
        
        
    } onQueue:nil];
    
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    } else {
        return 3;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    if (indexPath.section == 0) {
        HeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"headerCell"];
        
//        cell.imageView.image = [UIImage imageNamed:@"Icon"];
        return cell;
    } else {
        
        NSString *name = [self.array objectAtIndex:indexPath.row];
        
        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
        
        cell.name = name;
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return self.bounds.size.height / 7;
    } else {
        
        return self.bounds.size.height / 12;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 0;
    }else {
        
        return 20;
    }
}

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    
//    NSString *str = @"自定义";
//    return str;
//}


//当前控制器的导航控制器
- (UINavigationController*)naviController {
    
    for (UIView* next = [self superview]; next; next = next.superview) {
        
        UIResponder* nextResponder = [next nextResponder];
        
        if ([nextResponder isKindOfClass:[UINavigationController class]]) {
            
            return (UINavigationController*)nextResponder;
        }
    }
    
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        
        
//        MQLogonVCOfMinDy *VC = [[MQLogonVCOfMinDy alloc]init];
//        
//        
//        
//        [[self naviController] presentViewController:VC animated:YES completion:nil];

    if (indexPath.section == 1 && indexPath.row == 0) {
        FamilyViewController *FVC = [[FamilyViewController alloc] init];

        
        [[self naviController] pushViewController:FVC animated:YES];
    }else if (indexPath.section == 2  && indexPath.row == 0){
        
        
    //isUnbind是否解绑，如果是主动退出，那么传YES 被迫传NO
        
        [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES completion:^(NSDictionary *info, EMError *error) {
            
            if (!error) {
                
                AppDelegate *app = [UIApplication sharedApplication].delegate;
                
                [app  logOutSuccess];
                
                
            }
            
            
        } onQueue:nil];
       
        
    } else if (indexPath.section == 1 && indexPath.row == 0) {
        
        
        
    } else if (indexPath.section == 1 && indexPath.row == 1) {
        
        ScanViewController *scanVC = [[ScanViewController alloc] init];
        
        [[self naviController] pushViewController:scanVC animated:YES];
        
    } else if (indexPath.section == 1 && indexPath.row == 2) {
        
        MQDetailVCOfFind *detailVC = [[MQDetailVCOfFind alloc] init];
        
        [[self naviController] pushViewController:detailVC animated:YES];
        
    }
    
}
@end
